//二分法

var searchInsert = function(nums, target) {
    var low = 0, mid, high = nums.length, res = -1;
    while (low <= high) {
        mid = parseInt((low + high) / 2);
        if (nums[mid] === target) {
            res = mid;
            break;
        } else if (nums[mid] < target) {
            low = mid + 1;
        } else {
            high = mid - 1;
        }
    }
    if (res >= 0) {
        return res;
    } else {
        return low;
    }
};

console.log(searchInsert([1, 3, 5, 6], 5));